package it.avantsoft.ls.ui;

import it.avantsoft.ls.base.Application;
import it.avantsoft.ls.base.ApplicationTest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Set;

public class OutputForm extends JFrame implements Runnable {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JScrollPane scrollPane;
    private ColorPane outputTextArea;
    private String text;
    private boolean newContent;
    private String spacer = "";


    public OutputForm() {
        setContentPane(contentPane);
        //setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        text = "";
        newContent = false;
        spacer = "############################## - END OF COMMAND - ########################################";
        outputTextArea.setBackground(new Color(220, 220, 220));
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

// call onCancel() when cross is clicked
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

// call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);


    }

    public void printAllAddressBook() {


        addOutputString(spacer);

        //Stampa del server

        Application server = ApplicationTest.getServer();
        if (server != null) {
            server.printLAB();
            //addOutputString(server.printLAB());
            //addOutputString(server.printCAB());
            //addOutputString(server.printUAB());
            //addOutputString(server.printAPL());
        } else addSimpleString("Server --> Null");
        //Stampa dei peer
        java.util.List<Application> applications = ApplicationTest.getApplications();

        if (applications != null) {

            for (Application application : applications) {


                if (application != null && !application.getGroupLeaderHandler().isServer()) {
                    /* addOutputString(application.printLAB());

                                            addOutputString(application.printCAB());

                                            addOutputString(application.printUAB());

                                            addOutputString(application.printAPL());

                    */
                    addSimpleString("\n");
                    application.printLAB();
                    application.printCAB();
                    application.printUAB();
                    application.printAPL();
                    application.printIAP();
                    newContent = true;

                } else addSimpleString("Application --> NULL");

            }

        } else addSimpleString("Applications --> null");
    }

    private void onOK() {

        outputTextArea.setText("");
        newContent = false;
        dispose();
    }

    private void onCancel() {

        outputTextArea.setText("");
        newContent = false;
        dispose();
    }

    public void addOutputString(String s) {

        if (!text.equals(""))
            text += "\n";
        text += s;
        if (s != null && s.length() > 8) {
            String[] names = s.split("_");
            for (int i = 0; i < names.length; i++) {
                String name = names[i];

                if (name.length() > 2) {
                    outputTextArea.append(Color.BLACK, " ( ");
                    Color formatColor = getColorFromName(name.hashCode());
                    //System.out.println("red: "+red+" yellow: "+yellow+"blue: "+blue+" Name: "+name.hashCode());
                    outputTextArea.append(formatColor, name);
                    outputTextArea.append(Color.BLACK, " ) ");
                    if (i == 0)
                        outputTextArea.append(Color.BLACK, "-->");
                }


            }

            outputTextArea.append(Color.BLACK, "\n\n");
            if (!newContent)
                newContent = true;
        }
    }

    private Color getColorFromName(int hcode) {
        int red = Math.abs(hcode % 180);
        int green = Math.abs((hcode * 2) % 180);
        int blue = Math.abs((hcode * 3) % 180);
        return new Color(red, green, blue);  //To change body of created methods use File | Settings | File Templates.
    }

    public void paint(Graphics g) {

        super.paint(g);
    }


    public void run() {

        setMinimumSize(new Dimension(500, 500));
        pack();


        while (true) {


            if (newContent) {
                outputTextArea.validate();
                newContent = false;
                if (!isVisible())
                    this.setVisible(true);
            }

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }


    public void addSimpleString(String s) {
        outputTextArea.append(Color.BLACK, s);
    }

    public void addNameSet(String s, Set<String> labSet) {
        outputTextArea.append("(");


        outputTextArea.append(getColorFromName(s.subSequence(0, s.length() - 6).hashCode()), s.substring(0, s.length() - 6));
        outputTextArea.append(Color.BLACK, s.substring(s.length() - 5));
        outputTextArea.append(") (");
        for (String s1 : labSet) {
            if (s1.length() > 5) {
                outputTextArea.append(getColorFromName(s1.hashCode()), s1);
                outputTextArea.append(Color.BLACK, " - ");
            }
        }
        outputTextArea.append(Color.BLACK, ")\n");
    }
}

